package com.cain.imagefilter.ui;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.cain.imagefilter.FilterImage;
import com.cain.imagefilter.PhotoFilterFactory;
import com.cain.imagefilter.PhotoFilterFactory.PhotoFilterType;
import com.cain.imagefilter.R;

public class MainActivity extends Activity {
	
	private ImageView orgImageView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(this)
				.inflate(R.layout.main, null);
		
		orgImageView = (ImageView) linearLayout.findViewById(R.id.orgImage);
		Drawable drawable = orgImageView.getDrawable();
		Bitmap bitmap = Bitmap.createBitmap(
						drawable.getIntrinsicWidth(),
						drawable.getIntrinsicHeight(),
						drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
								: Bitmap.Config.RGB_565);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
				drawable.getIntrinsicHeight());
		drawable.draw(canvas);
		
		for(PhotoFilterType photoFilterType:PhotoFilterType.values()){
			Bitmap mBitmap = bitmap.copy(bitmap.getConfig(), true);
			ImageView filterImageView = new ImageView(this);
			filterImageView.setImageBitmap(getImage(photoFilterType, mBitmap));
			linearLayout.addView(filterImageView, new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT));
		}
		setContentView(linearLayout);
		
		
	}
	
	public Bitmap getImage(PhotoFilterType type,Bitmap bitmap){
		FilterImage filterImage = new FilterImage(bitmap);
		FilterImage tempFilterImage = PhotoFilterFactory.getInstance().createPhotoFilter(
				type).process(filterImage);
		return tempFilterImage.getImage();
	}
}
